capture log close

set matsize 5000


log using "LogFiles/WarsAndProsthesisPatentsAnalysisOfCounts.log", replace


**** Basic Time Series Figures
use "Data/all_patents_basicinfo.dta", clear
**** Analysis of issuance vs. filing lag. (Produces numbers added to the paper.)
sum iyear fyear if fyear != . & iyear <= 1922
sum iyear fyear if fyear != . & iyear <= 1922 & nclassgoogle1 == 623
sum iyear fyear if fyear != . & iyear >= 2000 & iyear <= 2022 & nclassgoogle1 == 623
sum iyear fyear if fyear != . & iyear >= 2000 & iyear <= 2022

**** Initial time series graphs for full database of patents

gen prosthetics = 0
replace prosthetics = 1 if nclassgoogle1 == 623
gen patentcount = 1
collapse (sum) patentcount, by(prosthetics iyear)
rename iyear year
tsset prosthetics year
tsfill, full

// Panel A, Figure D6: Patent Time Series 
twoway (tsline patentcount if year >= 1910 & year <= 1930 & prosthetics == 1, xline(1916, lcolor(black) lpattern(dash)) xline(1923, lcolor(black) lpattern(dash)) lwidth(medthick)) (tsline patentcount if year >= 1910 & year <= 1930 & prosthetics != 1, yaxis(2) lpattern(dash) lwidth(medthick)), ytitle(Prosthetic Patents) ytitle(All Other Patents, axis(2)) title(World War I Era Patents, color(black)) ylabel(0(15)45) ylabel(00000(30000)90000, axis(2)) xlabel(1910(5)1930) legend(label(1 "Prosthetics") label(2 "All Other")) graphregion(fcolor(white)) scale(0.9) name(LimbsAndAllOtherWWI, replace) 
graph export "Figures/LimbsAndAllOtherWWI.pdf", name(LimbsAndAllOtherWWI)  replace

use "Data/all_patents_basicinfo.dta", clear
**** Initial time series graphs for full database of patents
gen prosthetics = 0
replace prosthetics = 1 if nclassgoogle1 == 623
gen patentcount = 1
drop if fyear == .
collapse (sum) patentcount, by(prosthetics fyear)
rename fyear year
tsset prosthetics year
tsfill, full

// Panel B, Figure D6: Patent Time Series 
twoway (tsline patentcount if year >= 1910 & year <= 1930 & prosthetics == 1, xline(1916, lcolor(black) lpattern(dash)) xline(1923, lcolor(black) lpattern(dash)) lwidth(medthick)) (tsline patentcount if year >= 1910 & year <= 1930 & prosthetics != 1, yaxis(2) lpattern(dash) lwidth(medthick)), ytitle(Prosthetic Patents) ytitle(All Other Patents, axis(2)) title("World War I Era Patents (by Filing Year)", color(black)) ylabel(0(15)45) ylabel(00000(30000)90000, axis(2)) xlabel(1910(5)1930) legend(label(1 "Prosthetics") label(2 "All Other")) graphregion(fcolor(white)) scale(0.9) name(LimbsAndAllOtherWWIfyear, replace) 
graph export "Figures/LimbsAndAllOtherWWIfyear.pdf", name(LimbsAndAllOtherWWIfyear)  replace



**** Assemble data for additional figures and regressions
use "Data/control_classes_CW.dta", clear
append using "Data/control_classes_WWI.dta"
merge 1:1 patnum using "Data/all_patents_basicinfo.dta"
keep if _merge == 3
drop _merge

tab iyear if nclassgoogle1 == 623


*** Create Time series figures
preserve
gen prosthetics = 0
replace prosthetics = 1 if nclassgoogle1 == 623
gen patentcount = 1
collapse (sum) patentcount, by(prosthetics iyear)
rename iyear year
tsset prosthetics year
tsfill, full

// Panel A, Figure 1: Patent Time Series Contrasting Regions Directly Impacted by the US Civil War and World War I with Regions That Were Not
twoway (tsline patentcount if year >= 1855 & year <= 1875 & prosthetics == 1, xline(1862, lcolor(black) lpattern(dash)) xline(1867, lcolor(black) lpattern(dash)) lwidth(medthick)) (tsline patentcount if year >= 1855 & year <= 1875 & prosthetics != 1, yaxis(2) lpattern(dash) lwidth(medthick)), ytitle(Prosthetic Patents) ytitle(Control Patents, axis(2)) title(Civil War Era Patents, color(black)) ylabel(0(10)30) ylabel(0(3000)9000, axis(2)) xlabel(1855(5)1875) legend(label(1 "Prosthetics") label(2 "Control Patents")) graphregion(fcolor(white)) scale(0.9) name(LimbsAndControlsCivilWar, replace) 
graph export "Figures/LimbsAndControlsCivilWar.pdf", name(LimbsAndControlsCivilWar)  replace

// Panel C, Figure 1: Patent Time Series Contrasting Regions Directly Impacted by the US Civil War and World War I with Regions That Were Not
twoway (tsline patentcount if year >= 1910 & year <= 1930 & prosthetics == 1, xline(1916, lcolor(black) lpattern(dash)) xline(1923, lcolor(black) lpattern(dash)) lwidth(medthick)) (tsline patentcount if year >= 1910 & year <= 1930 & prosthetics != 1, yaxis(2) lpattern(dash) lwidth(medthick)), ytitle(Prosthetic Patents) ytitle(Control Patents, axis(2)) title(World War I Era Patents, color(black)) ylabel(0(15)45) ylabel(0000(15000)45000, axis(2)) xlabel(1910(5)1930) legend(label(1 "Prosthetics") label(2 "Control Patents")) graphregion(fcolor(white)) scale(0.9) name(LimbsAndControlsWWI, replace) 
graph export "Figures/LimbsAndControlsWWI.pdf", name(LimbsAndControlsWWI)  replace


restore



*** Generate a numeric variable for the individual war episodes
gen warepisode = .
replace warepisode = 1 if iyear >= 1840 & iyear <= 1890
replace warepisode = 2 if iyear >= 1890 & iyear <= 1940

*** Generate variables describe the "pre patent boom" period, the "patent 
*** boom" period, and the "post patent boom" period for the Civil War and WWI.
rename iyear year
gen period = .
replace period = 0 if year >= 1855 & year <= 1861
replace period = 1 if year >= 1862 & year <= 1866

replace period = 0 if year >= 1910 & year <= 1915
replace period = 1 if year >= 1916 & year <= 1922

*** Generate variable to indicate the number of years in each episode and period
bysort warepisode period: egen numyears=nvals(year)

tab numyears if warepisode == 1 & period == 0
tab numyears if warepisode == 1 & period == 1

gen usertraits = comfort + appearance  
gen production = adjustability + simplicity + cost + materials 
gen anyprodtraits = max(adjustability, simplicity, cost, materials) 

gen patentcount = 1

collapse (sum) patentcount (mean) numyears, by(nclassgoogle1 period warepisode)

gen patentsperyear = patentcount/numyears 
drop if patentsperyear <= 1
drop if period == 2
drop if period == .

*** Balance the class-by-episode panel
egen class_by_episode = group(nclassgoogle1 warepisode)
egen class_by_episodecount = count(class_by_episode), by(class_by_episode)
tab class_by_episodecount

drop if class_by_episodecount != 2

gen lnpatentsperyear = ln(patentsperyear)



*** Create key treatment variable
gen prosthetics = 0
replace prosthetics = 1 if nclassgoogle1 == 623

gen prosthetics_t_post = 0
replace prosthetics_t_post = 1 if period == 1 & prosthetics == 1 


*** Create Alternative Control Groups
gen medicalclass = 0
replace medicalclass = 1 if inlist(nclassgoogle1, 424, 514, 128, 600, 601, 602, 604, 606, 607, 435, 800, 351, 433, 623)
gen miscmechanical = 0
replace miscmechanical = 1 if inlist(nclassgoogle1, 7, 16, 42, 49, 51, 74, 81, 86, 89, 100, 124, 157, 184, 193, 194, 198, 212, 227, 235, 239, 254, 267, 291, 294, 384, 400, 402, 406, 411, 453, 454, 470, 482, 483, 492, 508)
gen metalworkmechanical = 0
replace metalworkmechanical = 1 if inlist(nclassgoogle1, 29, 72, 75, 76, 140, 147, 148, 163, 164, 228, 266, 270, 413, 419, 420)
gen materialprocessingmechanical = 0
replace materialprocessingmechanical = 1 if inlist(nclassgoogle1, 65, 82, 83, 125, 141, 142, 144, 173, 209, 221, 225, 226, 234, 241, 242, 264, 271, 407, 408, 409, 414, 425, 451, 493)
gen plausiblywarimpacted = 0
replace plausiblywarimpacted = 1 if inlist(nclassgoogle1, 12, 24, 26, 27, 28, 36, 37, 42, 59, 69, 86, 102, 104, 105, 114, 119, 124, 135, 149, 152, 172, 175, 180, 238, 246, 280, 295, 433, 600, 601, 602, 604, 606, 607)


gen patentsperyear0B = patentsperyear if period == 0
egen patentsperyear0 = max(patentsperyear0B), by(nclassgoogle1 warepisode)


areg lnpatentsperyear prosthetics_t_post i.period##i.warepisode if period < 2, absorb(class_by_episode)  cluster(class_by_episode)
estimates store r1
estadd local ClassPeriodeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local RandomizationInference = "P $< .01$"
estadd local Estimator = "OLS"

areg lnpatentsperyear prosthetics_t_post i.period##i.warepisode if period < 2 & ((warepisode == 1 & patentsperyear0 < 4) | (warepisode == 2 & patentsperyear0 < 20)), absorb(class_by_episode) cluster(class_by_episode)
estimates store r2
estadd local ClassPeriodeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local RandomizationInference = "P $< .01$"
estadd local Estimator = "OLS"

areg lnpatentsperyear prosthetics_t_post i.period##i.warepisode if period < 2 & medicalclass == 1, absorb(class_by_episode) cluster(class_by_episode)
estimates store r3
estadd local ClassPeriodeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local RandomizationInference = "P $< .01$"
estadd local Estimator = "OLS"

areg lnpatentsperyear prosthetics_t_post i.period##i.warepisode if period < 2 & (miscmechanical == 1  | nclassgoogle1 == 623), absorb(class_by_episode) cluster(class_by_episode)
estimates store r4
estadd local ClassPeriodeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local RandomizationInference = "P $< .01$"
estadd local Estimator = "OLS"

areg lnpatentsperyear prosthetics_t_post i.period##i.warepisode if period < 2 & (metalworkmechanical == 1 | nclassgoogle1 == 623), absorb(class_by_episode) cluster(class_by_episode)
estimates store r5
estadd local ClassPeriodeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local RandomizationInference = "P $< .01$"
estadd local Estimator = "OLS"

areg lnpatentsperyear prosthetics_t_post i.period##i.warepisode if period < 2 & (materialprocessingmechanical == 1 | nclassgoogle1 == 623), absorb(class_by_episode) cluster(class_by_episode)
estimates store r6
estadd local ClassPeriodeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local RandomizationInference = "P $< .01$"
estadd local Estimator = "OLS"

areg lnpatentsperyear prosthetics_t_post i.period##i.warepisode if period < 2 & (plausiblywarimpacted == 0 | nclassgoogle1 == 623), absorb(class_by_episode) cluster(class_by_episode)
estimates store r7
estadd local ClassPeriodeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local RandomizationInference = "P $< .01$"
estadd local Estimator = "OLS"


// Generating Table 4 (Panel A): Relative Increases in Prosthetic Device Patenting During the Civil War and World War I
estout r1 r2 r3 r4 r5 r6 r7 ///
    using "Tables/NumberOfPatentsLogSpecRegs.tex", replace ///
	keep(prosthetics_t_post) ///
	order(prosthetics_t_post) ///
	varlabels(prosthetics_t_post "Prosthetics x War Boom") ///
	mlabels("All Controls" "Matched" "Medical" "Misc. Mech." "Metal Works" "Mater. Proc." "Non War"   ///
		, span prefix(/multicolumn{@span}{c}{) suffix(})) ///
	cells("b(star label(Coeff.) fmt(%9.3f))" "se(par label(SE) fmt(%9.3f))") style(tex) nolegend  /// 
	stats(N N_clust Estimator ClassPeriodeffects Periodeffects StandardErrors RandomizationInference, fmt(%9.0fc %9.0fc) labels("\$N\$" "Clusters" "Estimator" "Class-by-Episode Effects" "Period Effects" "SEs in Parentheses" "Randomization Inference")) label starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001) ///
	prehead("\begin{tabular}{l*{@M}{cc}}" "\hline") posthead(/hline) ///
	prefoot(/hline) postfoot("\hline" "\end{tabular}")

estimates clear 


areg lnpatentsperyear prosthetics_t_post i.period if warepisode == 2 & period < 2, absorb(class_by_episode)  cluster(class_by_episode)
estimates store r1
estadd local ClassPeriodeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local RandomizationInference = "P $< .01$"
estadd local Estimator = "OLS"

areg lnpatentsperyear prosthetics_t_post i.period if warepisode == 2 & period < 2 & ((warepisode == 1 & patentsperyear0 < 4) | (warepisode == 2 & patentsperyear0 < 20)), absorb(class_by_episode) cluster(class_by_episode)
estimates store r2
estadd local ClassPeriodeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local RandomizationInference = "P $< .01$"
estadd local Estimator = "OLS"

areg lnpatentsperyear prosthetics_t_post i.period if warepisode == 2 & period < 2 & medicalclass == 1, absorb(class_by_episode) cluster(class_by_episode)
estimates store r3
estadd local Classeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local Estimator = "OLS"

areg lnpatentsperyear prosthetics_t_post i.period if warepisode == 2 & period < 2 & (miscmechanical == 1  | nclassgoogle1 == 623), absorb(class_by_episode) cluster(class_by_episode)
estimates store r4
estadd local Classeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local Estimator = "OLS"

areg lnpatentsperyear prosthetics_t_post i.period if warepisode == 2 & period < 2 & (metalworkmechanical == 1 | nclassgoogle1 == 623), absorb(class_by_episode) cluster(class_by_episode)
estimates store r5
estadd local Classeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local Estimator = "OLS"

areg lnpatentsperyear prosthetics_t_post i.period if warepisode == 2 & period < 2 & (materialprocessingmechanical == 1 | nclassgoogle1 == 623), absorb(class_by_episode) cluster(class_by_episode)
estimates store r6
estadd local Classeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local Estimator = "OLS"

areg lnpatentsperyear prosthetics_t_post i.period if warepisode == 2 & period < 2 & (plausiblywarimpacted == 0 | nclassgoogle1 == 623), absorb(class_by_episode) cluster(class_by_episode)
estimates store r7
estadd local Classeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local Estimator = "OLS"


// Generating Table 4 (Panel C): Relative Increases in Prosthetic Device Patenting During World War I
estout r1 r2 r3 r4 r5 r6 r7 ///
    using "Tables/NumberOfPatentsLogSpecRegsWWIOnly.tex", replace ///
	keep(prosthetics_t_post) ///
	order(prosthetics_t_post) ///
	varlabels(prosthetics_t_post "Prosthetics x War Boom") ///
	mlabels("All Controls" "Matched" "Medical" "Misc. Mech." "Metal Works" "Mater. Proc." "Non War"   ///
		, span prefix(\multicolumn{@span}{c}{) suffix(})) ///
	cells("b(star label(Coeff.) fmt(%9.3f))" "se(par label(SE) fmt(%9.3f))") style(tex) nolegend  /// 
	stats(N N_clust Estimator Classeffects Periodeffects StandardErrors , fmt(%9.0fc %9.0fc) labels("\$N\$" "Clusters" "Estimator" "Class Effects" "Period Effects" "SEs in Parentheses")) label starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001) ///
	prehead("\begin{tabular}{l*{@M}{cc}}" "\hline") posthead(\hline) ///
	prefoot(\hline) postfoot("\hline" "\end{tabular}")

estimates clear 



areg lnpatentsperyear prosthetics_t_post i.period if warepisode == 1 & period < 2, absorb(class_by_episode)  cluster(class_by_episode)
estimates store r1
estadd local ClassPeriodeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local RandomizationInference = "P $< .01$"
estadd local Estimator = "OLS"

areg lnpatentsperyear prosthetics_t_post i.period if warepisode == 1 & period < 2 & ((warepisode == 1 & patentsperyear0 < 4) | (warepisode == 2 & patentsperyear0 < 20)), absorb(class_by_episode) cluster(class_by_episode)
estimates store r2
estadd local ClassPeriodeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local RandomizationInference = "P $< .01$"
estadd local Estimator = "OLS"

areg lnpatentsperyear prosthetics_t_post i.period if warepisode == 1 & period < 2 & medicalclass == 1, absorb(class_by_episode) cluster(class_by_episode)
estimates store r3
estadd local Classeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local Estimator = "OLS"

areg lnpatentsperyear prosthetics_t_post i.period if warepisode == 1 & period < 2 & (miscmechanical == 1  | nclassgoogle1 == 623), absorb(class_by_episode) cluster(class_by_episode)
estimates store r4
estadd local Classeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local Estimator = "OLS"

areg lnpatentsperyear prosthetics_t_post i.period if warepisode == 1 & period < 2 & (metalworkmechanical == 1 | nclassgoogle1 == 623), absorb(class_by_episode) cluster(class_by_episode)
estimates store r5
estadd local Classeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local Estimator = "OLS"

areg lnpatentsperyear prosthetics_t_post i.period if warepisode == 1 & period < 2 & (materialprocessingmechanical == 1 | nclassgoogle1 == 623), absorb(class_by_episode) cluster(class_by_episode)
estimates store r6
estadd local Classeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local Estimator = "OLS"

areg lnpatentsperyear prosthetics_t_post i.period if warepisode == 1 & period < 2 & (plausiblywarimpacted == 0 | nclassgoogle1 == 623), absorb(class_by_episode) cluster(class_by_episode)
estimates store r7
estadd local Classeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local Estimator = "OLS"


// Generating Table 4 (Panel B): Relative Increases in Prosthetic Device Patenting During the Civil War
estout r1 r2 r3 r4 r5 r6 r7 ///
    using "Tables/NumberOfPatentsLogSpecRegsCIVOnly.tex", replace ///
	keep(prosthetics_t_post) ///
	order(prosthetics_t_post) ///
	varlabels(prosthetics_t_post "Prosthetics x War Boom") ///
	mlabels("All Controls" "Matched" "Medical" "Misc. Mech." "Metal Works" "Mater. Proc." "Non War"   ///
		, span prefix(\multicolumn{@span}{c}{) suffix(})) ///
	cells("b(star label(Coeff.) fmt(%9.3f))" "se(par label(SE) fmt(%9.3f))") style(tex) nolegend  /// 
	stats(N N_clust Estimator Classeffects Periodeffects StandardErrors , fmt(%9.0fc %9.0fc) labels("\$N\$" "Clusters" "Estimator" "Class Effects" "Period Effects" "SEs in Parentheses")) label starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001) ///
	prehead("\begin{tabular}{l*{@M}{cc}}" "\hline") posthead(\hline) ///
	prefoot(\hline) postfoot("\hline" "\end{tabular}")

estimates clear 


poisson patentsperyear prosthetics_t_post i.period##i.warepisode i.nclassgoogle1##i.warepisode if period < 2, cluster(class_by_episode)
estimates store r1
estadd local ClassPeriodeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local RandomizationInference = "P $< .01$"
estadd local Estimator = "Poisson"

poisson patentsperyear prosthetics_t_post i.period##i.warepisode i.nclassgoogle1##i.warepisode if period < 2 & ((warepisode == 1 & patentsperyear0 < 4) | (warepisode == 2 & patentsperyear0 < 20)), cluster(class_by_episode)
estimates store r2
estadd local ClassPeriodeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local RandomizationInference = "P $< .01$"
estadd local Estimator = "Poisson"

poisson patentsperyear prosthetics_t_post i.period##i.warepisode i.nclassgoogle1##i.warepisode if period < 2 & medicalclass == 1, cluster(class_by_episode)
estimates store r3
estadd local ClassPeriodeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local RandomizationInference = "P $< .01$"
estadd local Estimator = "Poisson"

poisson patentsperyear prosthetics_t_post i.period##i.warepisode i.nclassgoogle1##i.warepisode if period < 2 & (miscmechanical == 1  | nclassgoogle1 == 623), cluster(class_by_episode)
estimates store r4
estadd local ClassPeriodeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local RandomizationInference = "P $< .01$"
estadd local Estimator = "Poisson"

poisson patentsperyear prosthetics_t_post i.period##i.warepisode i.nclassgoogle1##i.warepisode if period < 2 & (metalworkmechanical == 1 | nclassgoogle1 == 623), cluster(class_by_episode)
estimates store r5
estadd local ClassPeriodeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local RandomizationInference = "P $< .01$"
estadd local Estimator = "Poisson"

poisson patentsperyear prosthetics_t_post i.period##i.warepisode i.nclassgoogle1##i.warepisode if period < 2 & (materialprocessingmechanical == 1 | nclassgoogle1 == 623), cluster(class_by_episode)
estimates store r6
estadd local ClassPeriodeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local RandomizationInference = "P $< .01$"
estadd local Estimator = "Poisson"

poisson patentsperyear prosthetics_t_post i.period##i.warepisode i.nclassgoogle1##i.warepisode if period < 2 & (plausiblywarimpacted == 0 | nclassgoogle1 == 623), cluster(class_by_episode)
estimates store r7
estadd local ClassPeriodeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local RandomizationInference = "P $< .01$"
estadd local Estimator = "Poisson"

// Genarating Table D.1 (Panel A): Relative Increases in Prosthetic Device Patenting During the Civil War and World War I
estout r1 r2 r3 r4 r5 r6 r7 ///
    using "Tables/NumberOfPatentsPoissonSpecRegs.tex", replace ///
	keep(prosthetics_t_post) ///
	order(prosthetics_t_post) ///
	varlabels(prosthetics_t_post "Prosthetics x War Boom") ///
	mlabels("All Controls" "Matched" "Medical" "Misc. Mech." "Metal Works" "Mater. Proc." "Non War"  ///
		, span prefix(/multicolumn{@span}{c}{) suffix(})) ///
	cells("b(star label(Coeff.) fmt(%9.3f))" "se(par label(SE) fmt(%9.3f))") style(tex) nolegend  /// 
	stats(N N_clust Estimator ClassPeriodeffects Periodeffects StandardErrors RandomizationInference, fmt(%9.0fc %9.0fc) labels("\$N\$" "Clusters" "Estimator" "Class-by-Episode Effects" "Period Effects" "SEs in Parentheses" "Randomization Inference")) label starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001) ///
	prehead("\begin{tabular}{l*{@M}{cc}}" "\hline") posthead(/hline) ///
	prefoot(/hline) postfoot("\hline" "\end{tabular}")

estimates clear 

poisson patentsperyear prosthetics_t_post i.period i.nclassgoogle1 if warepisode == 2 & period < 2 , cluster(class_by_episode)
estimates store r1
estadd local Classeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local Estimator = "OLS"

poisson patentsperyear prosthetics_t_post i.period i.nclassgoogle1 if warepisode == 2 & period < 2  & ((warepisode == 1 & patentsperyear0 < 4) | (warepisode == 2 & patentsperyear0 < 20)), cluster(class_by_episode)
estimates store r2
estadd local Classeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local Estimator = "OLS"

poisson patentsperyear prosthetics_t_post i.period i.nclassgoogle1 if warepisode == 2 & period < 2  & medicalclass == 1, cluster(class_by_episode)
estimates store r3
estadd local Classeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local Estimator = "OLS"

poisson patentsperyear prosthetics_t_post i.period i.nclassgoogle1 if warepisode == 2 & period < 2  & (miscmechanical == 1  | nclassgoogle1 == 623), cluster(class_by_episode)
estimates store r4
estadd local Classeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local Estimator = "OLS"

poisson patentsperyear prosthetics_t_post i.period i.nclassgoogle1 if warepisode == 2 & period < 2  & (metalworkmechanical == 1 | nclassgoogle1 == 623), cluster(class_by_episode)
estimates store r5
estadd local Classeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local Estimator = "OLS"

poisson patentsperyear prosthetics_t_post i.period i.nclassgoogle1 if warepisode == 2 & period < 2  & (materialprocessingmechanical == 1 | nclassgoogle1 == 623), cluster(class_by_episode)
estimates store r6
estadd local Classeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local Estimator = "OLS"

poisson patentsperyear prosthetics_t_post i.period i.nclassgoogle1 if warepisode == 2 & period < 2 & (plausiblywarimpacted == 0 | nclassgoogle1 == 623), cluster(class_by_episode)
estimates store r7
estadd local Classeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local Estimator = "OLS"

// Genarating Table D.1 (Panel C): Relative Increases in Prosthetic Device Patenting During World War I
estout r1 r2 r3 r4 r5 r6 r7 ///
    using "Tables/NumberOfPatentsPoissonSpecRegsWWIOnly.tex", replace ///
	keep(prosthetics_t_post) ///
	order(prosthetics_t_post) ///
	varlabels(prosthetics_t_post "Prosthetics x War Boom") ///
	mlabels("All Controls" "Matched" "Medical" "Misc. Mech." "Metal Works" "Mater. Proc." "Non War"  ///
		, span prefix(\multicolumn{@span}{c}{) suffix(})) ///
	cells("b(star label(Coeff.) fmt(%9.3f))" "se(par label(SE) fmt(%9.3f))") style(tex) nolegend  /// 
	stats(N N_clust Estimator Classeffects Periodeffects StandardErrors, fmt(%9.0fc %9.0fc) labels("\$N\$" "Clusters" "Estimator" "Class Effects" "Period Effects" "SEs in Parentheses")) label starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001) ///
	prehead("\begin{tabular}{l*{@M}{cc}}" "\hline") posthead(\hline) ///
	prefoot(\hline) postfoot("\hline" "\end{tabular}")

estimates clear 


poisson patentsperyear prosthetics_t_post i.period i.nclassgoogle1 if warepisode == 1 & period < 2 , cluster(class_by_episode)
estimates store r1
estadd local Classeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local Estimator = "OLS"

poisson patentsperyear prosthetics_t_post i.period i.nclassgoogle1 if warepisode == 1 & period < 2  & ((warepisode == 1 & patentsperyear0 < 4) | (warepisode == 2 & patentsperyear0 < 20)), cluster(class_by_episode)
estimates store r2
estadd local Classeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local Estimator = "OLS"

poisson patentsperyear prosthetics_t_post i.period i.nclassgoogle1 if warepisode == 1 & period < 2  & medicalclass == 1, cluster(class_by_episode)
estimates store r3
estadd local Classeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local Estimator = "OLS"

poisson patentsperyear prosthetics_t_post i.period i.nclassgoogle1 if warepisode == 1 & period < 2  & (miscmechanical == 1  | nclassgoogle1 == 623), cluster(class_by_episode)
estimates store r4
estadd local Classeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local Estimator = "OLS"

poisson patentsperyear prosthetics_t_post i.period i.nclassgoogle1 if warepisode == 1 & period < 2  & (metalworkmechanical == 1 | nclassgoogle1 == 623), cluster(class_by_episode)
estimates store r5
estadd local Classeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local Estimator = "OLS"

poisson patentsperyear prosthetics_t_post i.period i.nclassgoogle1 if warepisode == 1 & period < 2  & (materialprocessingmechanical == 1 | nclassgoogle1 == 623), cluster(class_by_episode)
estimates store r6
estadd local Classeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local Estimator = "OLS"

poisson patentsperyear prosthetics_t_post i.period i.nclassgoogle1 if warepisode == 1 & period < 2 & (plausiblywarimpacted == 0 | nclassgoogle1 == 623), cluster(class_by_episode)
estimates store r7
estadd local Classeffects = "Yes"
estadd local Periodeffects = "Yes"
estadd local StandardErrors = "Clustered"
estadd local Estimator = "OLS"

// Genarating Table D.1 (Panel B): Relative Increases in Prosthetic Device Patenting During the Civil War

estout r1 r2 r3 r4 r5 r6 r7 ///
    using "Tables/NumberOfPatentsPoissonSpecRegsCIVOnly.tex", replace ///
	keep(prosthetics_t_post) ///
	order(prosthetics_t_post) ///
	varlabels(prosthetics_t_post "Prosthetics x War Boom") ///
	mlabels("All Controls" "Matched" "Medical" "Misc. Mech." "Metal Works" "Mater. Proc." "Non War"  ///
		, span prefix(\multicolumn{@span}{c}{) suffix(})) ///
	cells("b(star label(Coeff.) fmt(%9.3f))" "se(par label(SE) fmt(%9.3f))") style(tex) nolegend  /// 
	stats(N N_clust Estimator Classeffects Periodeffects StandardErrors, fmt(%9.0fc %9.0fc) labels("\$N\$" "Clusters" "Estimator" "Class Effects" "Period Effects" "SEs in Parentheses")) label starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001) ///
	prehead("\begin{tabular}{l*{@M}{cc}}" "\hline") posthead(\hline) ///
	prefoot(\hline) postfoot("\hline" "\end{tabular}")

estimates clear 


	
	
drop patentsperyear0B patentsperyear0	
drop class_by_episode prosthetics_t_post prosthetics
reshape wide patentcount patentsperyear lnpatentsperyear numyears , i(nclassgoogle1 warepisode) j(period)  

foreach var in patentsperyear lnpatentsperyear {
	gen change`var' = `var'1 - `var'0 
}

sum changelnpatentsperyear, detail
sum changelnpatentsperyear if nclassgoogle1 == 623

sum changelnpatentsperyear if warepisode == 1, detail
sum changelnpatentsperyear if warepisode == 1 & nclassgoogle1 == 623

sum changelnpatentsperyear if warepisode == 2, detail
sum changelnpatentsperyear if warepisode == 2 & nclassgoogle1 == 623

drop if changelnpatentsperyear == .

gen prosthetics = nclassgoogle1 == 623
reg changelnpatentsperyear prosthetics, robust
reg changelnpatentsperyear prosthetics i.warepisode, robust




reg changelnpatentsperyear prosthetics if (warepisode == 1 & patentsperyear0 < 4) | (warepisode == 2 & patentsperyear0 < 20), robust
reg changelnpatentsperyear prosthetics if medicalclass == 1, robust
reg changelnpatentsperyear prosthetics if miscmechanical == 1  | nclassgoogle1 == 623, robust
reg changelnpatentsperyear prosthetics if metalworkmechanical == 1 | nclassgoogle1 == 623, robust
reg changelnpatentsperyear prosthetics if materialprocessingmechanical == 1 | nclassgoogle1 == 623, robust

reg changelnpatentsperyear prosthetics i.warepisode if (warepisode == 1 & patentsperyear0 < 4) | (warepisode == 2 & patentsperyear0 < 20), robust
reg changelnpatentsperyear prosthetics i.warepisode if medicalclass == 1, robust
reg changelnpatentsperyear prosthetics i.warepisode if miscmechanical == 1  | nclassgoogle1 == 623, robust
reg changelnpatentsperyear prosthetics i.warepisode if metalworkmechanical == 1 | nclassgoogle1 == 623, robust
reg changelnpatentsperyear prosthetics i.warepisode if materialprocessingmechanical == 1 | nclassgoogle1 == 623, robust


// Generating Panel A, Figure D.2: Patents in Prosthetic Devices and Mechanical Classes
label var changelnpatentsperyear "Change in ln(Patent Count)"
*** Distributions for each episode
sum changelnpatentsperyear if nclassgoogle1 == 623 & warepisode == 1
hist changelnpatentsperyear if warepisode == 1 & nclassgoogle1 != 623, scale(0.9) title(Distribution of Changes During Civil War, size(medlarge)) color(white)  blcolor(red) width(.1) xline(`r(mean)', lcolor(black) lpattern(dash)) frac xlabel(-1.5(.5)2.5) graphregion(color(white)) name(CivilWarHist, replace)
graph export "Figures/CivilWarHist.pdf", replace name(CivilWarHist)

// Generating Panel A, Figure D.2: Patents in Prosthetic Devices and Mechanical Classes
sum changelnpatentsperyear if nclassgoogle1 == 623 & warepisode == 2
hist changelnpatentsperyear if warepisode == 2 & nclassgoogle1 != 623, scale(0.9) title(Distribution of Changes During World War I, size(medlarge)) color(white)  blcolor(red) width(.1) xline(`r(mean)', lcolor(black) lpattern(dash)) frac xlabel(-2.0(.5)2.0) graphregion(color(white)) name(WorldWarIHist, replace)
graph export "Figures/WorldWarIHist.pdf", replace name(WorldWarIHist)




// Now move to generate Figure D.3: Placebo Point Estimate Distributions across Three Algorithms
set seed 9999
*** Loop A.  Assignment at random to 2 out of all class-episode combinations
forval i = 1/500 { 
	
	gen x =rnormal(0,1) if nclassgoogle1 != 623
	egen rankx = rank(x)
	gen placebotreatment = rankx <= 2
	tab placebotreatment

	reg changelnpatentsperyear placebotreatment i.warepisode, robust
	parmest,format(estimate min95 max95 %8.3f p %8.1e) saving("Estimates/Placebo`i'A.dta", replace)
	
	drop x rankx placebotreatment

}

reg changelnpatentsperyear prosthetics i.warepisode, robust
parmest,format(estimate min95 max95 %8.3f p %8.1e) saving("Estimates/TrueEstimateA.dta", replace)



*** Loop B.  Assignment at random to 1 class from each war episode
forval i = 1/500 { 
	
	gen x =rnormal(0,1) if nclassgoogle1 != 623
	egen rankx = rank(x), by(warepisode) 
	gen placebotreatment = rankx <= 1
	tab placebotreatment

	reg changelnpatentsperyear placebotreatment i.warepisode, robust
	parmest,format(estimate min95 max95 %8.3f p %8.1e) saving("Estimates/Placebo`i'B.dta", replace)
	
	drop x rankx placebotreatment

}

reg changelnpatentsperyear prosthetics i.warepisode, robust
parmest,format(estimate min95 max95 %8.3f p %8.1e) saving("Estimates/TrueEstimateB.dta", replace)



*** Loop C.  Assignment at random to 1 class, with treatment then assigned for both war episodes 
*** Sample restricted to classes that appear in both episodes
preserve
egen classcount = count(nclassgoogle1), by(nclassgoogle1)
tab classcount 
drop if classcount == 1
forval i = 1/500 { 
	
	gen x =rnormal(0,1) if nclassgoogle1 != 623
	egen rankx = rank(x), by(warepisode) 
	gen placebotreatmentB = rankx <= 1 & warepisode == 1
	egen placebotreatment = max(placebotreatmentB), by(nclassgoogle1) 
	tab placebotreatment

	reg changelnpatentsperyear placebotreatment i.warepisode, robust
	parmest,format(estimate min95 max95 %8.3f p %8.1e) saving("Estimates/Placebo`i'C.dta", replace)
	
	drop x rankx placebotreatment placebotreatmentB

}
reg changelnpatentsperyear prosthetics i.warepisode, robust
parmest,format(estimate min95 max95 %8.3f p %8.1e) saving("Estimates/TrueEstimateC.dta", replace)

restore



preserve 


use "Estimates/Placebo1A.dta", clear
forval i = 2/500 {
	append using "Estimates/Placebo`i'A.dta"	
}
append using "Estimates/TrueEstimateA.dta"	

label var estimate "Estimate on Randomly Assigned Treatment Indicator"
sum estimate if parm == "placebotreatment", detail
sum estimate if parm == "prosthetics"
hist estimate if parm == "placebotreatment", scale(0.9) title(Distribution for Assignment Algorithm A, size(medlarge)) color(white)  blcolor(red) width(.05) xline(`r(mean)', lcolor(black) lpattern(dash)) frac xlabel(-1.5(.5)1.5) graphregion(color(white)) name(PlaceboCountsHistA, replace)
// Panel A, Figure D.3: Placebo Point Estimate Distributions across Three Algorithms
graph export "Figures/PlaceboCountsHistA.pdf", replace name(PlaceboCountsHistA)


use "Estimates/Placebo1B.dta", clear
forval i = 2/500 {
	append using "Estimates/Placebo`i'B.dta"	
}
append using "Estimates/TrueEstimateB.dta"	

label var estimate "Estimate on Randomly Assigned Treatment Indicator"
sum estimate if parm == "placebotreatment", detail
sum estimate if parm == "prosthetics"
hist estimate if parm == "placebotreatment", scale(0.9) title(Distribution for Assignment Algorithm B, size(medlarge)) color(white)  blcolor(red) width(.05) xline(`r(mean)', lcolor(black) lpattern(dash)) frac xlabel(-1.5(.5)1.5) graphregion(color(white)) name(PlaceboCountsHistB, replace)
// Panel B, Figure D.3: Placebo Point Estimate Distributions across Three Algorithms
graph export "Figures/PlaceboCountsHistB.pdf", replace name(PlaceboCountsHistB)


use "Estimates/Placebo1C.dta", clear
forval i = 2/500 {
	append using "Estimates/Placebo`i'C.dta"	
}
append using "Estimates/TrueEstimateC.dta"	

label var estimate "Estimate on Randomly Assigned Treatment Indicator"
sum estimate if parm == "placebotreatment", detail
sum estimate if parm == "prosthetics"
hist estimate if parm == "placebotreatment", scale(0.9) title(Distribution for Assignment Algorithm C, size(medlarge)) color(white)  blcolor(red) width(.05) xline(`r(mean)', lcolor(black) lpattern(dash)) frac xlabel(-1.5(.5)1.5) graphregion(color(white)) name(PlaceboCountsHistC, replace)
// Panel C, Figure D.3: Placebo Point Estimate Distributions across Three Algorithms
graph export "Figures/PlaceboCountsHistC.pdf", replace name(PlaceboCountsHistC)

//Figure D.3 Combined: Placebo Point Estimate Distributions across Three Algorithms
graph combine PlaceboCountsHistA PlaceboCountsHistB PlaceboCountsHistC, cols(2) imargin(small) scale(.9) graphregion(color(white)) name(ThreePanelInferenceAlgorithms, replace) ///
	title("Placebo Point Estimate Distributions across Three Algorithms")
graph export "Figures/ThreePanelInferenceAlgorithms.pdf", name(ThreePanelInferenceAlgorithms) replace


restore



forval i = 1/500 {
	capture rm "Estimates/Placebo`i'A.dta"
	capture rm "Estimates/Placebo`i'B.dta"
	capture rm "Estimates/Placebo`i'C.dta"
}

capture log close



capture log close
